package com.tianqu.tms.www.car.controller;

import java.util.List;

import com.tianqu.tms.core.base.Logger;
import com.tianqu.tms.core.base.TmsController;
import com.tianqu.tms.core.base.TxController;
import com.tianqu.tms.core.base.UserContext;
import com.tianqu.tms.core.exception.TmsException;
import com.tianqu.tms.core.exception.TransactionException;
import com.tianqu.tms.entity.Car;
import com.tianqu.tms.www.car.action.CarInnerListTopAction;

public class CarInnerListTopController extends TmsController {

	public static final String INNERCARLIST = "innerCarList";
	public static final String PAGING = "paging";

	@SuppressWarnings("unchecked")
	@Override
	public void execute(UserContext uc) throws Exception {
		Logger logger = Logger.getLogger(CarInnerListTopController.class);

		logger.debug(this.getClass().getName() + "#execute was called.");
		TxController controller = TxController.getTxController();

		// 添加事务提交异常捕获
		try {
			controller.begin();
			Car car = Car.getInstance();
			//
			if (uc.getAttribute(CarInnerListTopAction.PAGENO) != null) {
				car.setPageNo(Integer.parseInt((String) uc
						.getAttribute(CarInnerListTopAction.PAGENO)));
			}
			if (uc.getAttribute(CarInnerListTopAction.MAXINDEX) != null) {
				car.setMaxIndex(Integer.parseInt((String) uc
						.getAttribute(CarInnerListTopAction.MAXINDEX)));
			}
			if (uc.getAttribute(CarInnerListTopAction.SORTKEY) != null) {
				car.setSortKey(Integer.parseInt((String) uc
						.getAttribute(CarInnerListTopAction.SORTKEY)));
			}
			if (uc.getAttribute(CarInnerListTopAction.ASC) != null) {
				car.setAsc((new Boolean((String) uc
						.getAttribute(CarInnerListTopAction.ASC)))
						.booleanValue());
			}
			List innerCarList = car.loadInnerCarList(); 
			controller.commit();

			uc.setAttribute(INNERCARLIST, innerCarList);
			uc.setAttribute(PAGING, car);
		} catch (Exception e) {
			try {
				controller.rollback();
			} catch (TransactionException e1) {
				throw new TmsException(TmsException.DB_TXCRROR, e1);
			}
			// 系统异常
			if (e instanceof TmsException) {
				throw e;
			}
			throw new TmsException(TmsException.DB_TXCRROR, e);
		}

	}

}
